package com.lesserpand.rabbitmq.threadpool.thread;

import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author pand
 * @version 0.0.1
 * @package com.topinfo.locationservice.utils.thread
 * @description 线程池工厂，即线程池没有可用线程，自动创建一个线程。前提是池子没满
 * @date Created in 2019/4/1 15:00
 * @company
 * @copyright Copyright (c) 2017
 * @modified By
 */
@Component
public class SyncThreadFactory implements ThreadFactory {
    private final ThreadGroup group;
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;

    SyncThreadFactory() {
        SecurityManager var1 = System.getSecurityManager();
        this.group = var1 != null ? var1.getThreadGroup() : Thread.currentThread().getThreadGroup();
        this.namePrefix = "pool-1-thread-";
    }

    @Override
    public Thread newThread(Runnable var1) {
        Thread thread = new Thread(this.group, var1, this.namePrefix + this.threadNumber.getAndIncrement(), 0L);
        if (thread.isDaemon()) {
            thread.setDaemon(false);
        }

        if (thread.getPriority() != 5) {
            thread.setPriority(5);
        }

        return thread;
    }
}
